package org.dromara.third.convert.gasAnalysis.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.GasAnalysis;
import org.dromara.system.mapper.GasAnalysisMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.gasAnalysis.dto.GasAnalysisDTO;
import org.dromara.third.convert.gasAnalysis.validator.GasAnalysisValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertGasAnalysisController {

    @Autowired
    private GasAnalysisMapper gasAnalysisMapper;

    @PostMapping("/gas")
    @Transactional
    public R<Void> addGasAnalysis(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        GasAnalysisDTO gasAnalysisDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            gasAnalysisDTO = JSONObject.parseObject(decrypt, GasAnalysisDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, gasAnalysisDTO, new GasAnalysisValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        GasAnalysis gasAnalysis = assembleGasAnalysisData(orgCode, gasAnalysisDTO);
        int count = gasAnalysisMapper.countWithOutDelFlag(gasAnalysis.getId());
        if (count == 0) {
            gasAnalysisMapper.insert(gasAnalysis);
        } else {
            gasAnalysisMapper.updateWithOutDelFlag(gasAnalysis);
        }
        return R.ok();
    }

    private GasAnalysis assembleGasAnalysisData(String orgCode, GasAnalysisDTO dto) {
        GasAnalysis gasAnalysis = new GasAnalysis();
        try {
            gasAnalysis.setId(dto.getDataId());
            gasAnalysis.setZyid(dto.getZyid());
            gasAnalysis.setZylx(dto.getZylx());
            gasAnalysis.setQtlx(dto.getQtlx());
            gasAnalysis.setQtmc(dto.getQtmc());
            gasAnalysis.setFxjg(dto.getFxjg());
            gasAnalysis.setJgdw(dto.getJgdw());
            gasAnalysis.setFxr(dto.getFxr());
            gasAnalysis.setFxsj(DateUtils.parseDate(dto.getFxsj()));
            gasAnalysis.setCyfxbw(dto.getCyfxbw());
            gasAnalysis.setCash(dto.getCash());
            gasAnalysis.setJcsfhg(dto.getJcsfhg());
            gasAnalysis.setQmtp(dto.getQmtp());
            gasAnalysis.setDelFlag(Long.parseLong(dto.getDelFlag()));
            gasAnalysis.setCreateDept(Long.parseLong(dto.getCreateDept()));
            gasAnalysis.setCreateBy(Long.parseLong(dto.getCreateBy()));
            gasAnalysis.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            gasAnalysis.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            gasAnalysis.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            gasAnalysis.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装气体分析数据异常");
        }
        return gasAnalysis;
    }
}
